<?php
class top extends spController{
	function index(){
		$sessions = spClass('m_top') -> getSessions();
		?>
		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
		<html xmlns="http://www.w3.org/1999/xhtml">
		<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		</head>
		
		<body>
		<p><a href="?c=top&a=call">点此绑定淘宝帐户，以便开展业务</a></p>
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
		  <tr>
		    <th width="33%" scope="col">ID</th>
		    <th width="33%" scope="col">绑定淘宝卖家</th>
		    <th width="33%" scope="col">解绑</th>
		  </tr>
		  <?php for($i=0;isset($sessions[$i]);$i++){?>
		  <tr>
		    <td width="33%"><?php echo $sessions[$i]['id'] ?></td>
		    <td width="33%"><?php echo $sessions[$i]['nick'] ?></td>
		    <td width="33%"><a href="?c=top&a=delete&id=<?php echo $sessions[$i]['id'] ?>">点此解除此卖家绑定</a></td>
		  </tr>
		  <?php }?>
		</table>

		</body>
		</html>
				
		<?php
	}
	
	function call(){
		$appkeys = unserialize(spAccess('r','appkeys'));
		$i=0;$m= mt_rand(0, count($appkeys)-1);
		foreach ($appkeys as $k => $v){
			if($i == $m){
				$this->jump('http://container.api.taobao.com/container?appkey='.$k);
			}
		}
	}
	
	function delete(){
		$sessions = spClass('m_top') -> getSessions($this -> spArgs('id'));
		spClass('m_top') -> sessionInvilid($sessions[0]['nick']);
		$this -> jump(spUrl('top','index'));
	}
		
	
	function back(){
		$info = base64_decode($this -> spArgs('top_parameters'));
		parse_str($info,$infos);
		$nick = $infos['visitor_nick'];
		$sessionkey = $this -> spArgs('top_session');
		spClass('m_top') -> sessionUpdate($nick,$sessionkey,$this -> spArgs('top_appkey'));
		$this -> jump(spUrl('top'));
	}
	
	function appkey_add(){
		$appkey = $this->spArgs('appkey');$appsecret = $this -> spArgs('appsecret');
		$appkeys = unserialize(spAccess('r','appkeys'));
		if(empty($appkeys)){
			$appkeys = array();
		}
		$appkeys[$appkey] = $appsecret;
		spAccess('w', 'appkeys',serialize($appkeys));
		$this -> jump(spUrl('top','appkey'));
	}
	
	function appkey_delete(){
		$appkey = $this->spArgs('appkey');
		$appkeys = unserialize(spAccess('r','appkeys'));
		unset($appkeys[$appkey]);
		spAccess('w', serialize($appkeys));
		$this -> jump(spUrl('top','appkey'));
	}
	
	function appkey(){
		$appkeys = unserialize(spAccess('r','appkeys'));
		?>
		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
		<html xmlns="http://www.w3.org/1999/xhtml">
		<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>APPKEY</title>
		</head>
		
		<body>
		<table width="760" border="0" align="center" cellpadding="0" cellspacing="0">
		  <tr>
		    <th width="33%" scope="col">Appkey</th>
		    <th width="33%" scope="col">Appsecret</th>
		    <th width="33%" scope="col">删除</th>
		  </tr>
		  <?php foreach($appkeys as $k => $v){ ?>
		  <tr>
		    <td width="33%"><?php echo $k ?></td>
		    <td width="33%"><?php echo $v ?></td>
		    <td width="33%"><a href="?c=top&a=appkey_delete&appkey=<?php echo $k ?>">删除</a></td>
		  </tr>
		  <?php 
		  }
		  ?>
		  <form action="?c=top&a=appkey_add" method="POST">
		  <tr>
		    <td align="center"><label>
		      <input type="text" name="appkey" id="textfield" />
		    </label></td>
		    <td align="center"><input type="text" name="appsecret" id="textfield2" /></td>
		    <td align="center"><label>
		      <input type="submit" name="button" id="button" value="提交" />
		    </label></td>
		  </tr>
		  </form>
		</table>
		</body>
		</html>
				
		<?php 
	}
}